<template>
  <div>
    <el-card>
      <upload-excel :on-success="handleSuccess" />
    </el-card>
  </div>
</template>

<script>
import { formatExcelDate } from '@/utils/index'
import { importEmployee } from '@/api/employees'
export default {
  name: 'Excel',
  methods: {

    transExcel(results) {
    // 枚举用来把名字转成英文的
      const userRelations = {
        '入职日期': 'timeOfEntry',
        '手机号': 'mobile',
        '姓名': 'username',
        '转正日期': 'correctionTime',
        '工号': 'workNumber',
        '部门': 'departmentName',
        '聘用形式': 'formOfEmployment'
      }
      return results.map(item => {
        const obj = {}
        const contentKeys = Object.keys(item) // 用这个方法取出所有的key也就是名字
        // contentKeys = ["姓名", "手机号", "入职日期", "转正日期", "工号", "部门"]
        contentKeys.forEach(key => {
          const transKey = userRelations[key]
          if (transKey === 'timeOfEntry' || transKey === 'correctionTime') {
            obj[transKey] = new Date(formatExcelDate(item[key]))
          } else {
            obj[transKey] = item[key]
          }
        })
        return obj
      })
    },
    async handleSuccess({ results, header }) {
      const arr = this.transExcel(results)
      const res = await importEmployee(arr)
      this.$message.success(res.message)
      this.$router.back()
    }

  }
}
</script>

<style>

</style>
